package com.bookcloud.java21.chapter9.读取控制台输入95;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/** 读取控制台输入 */
public class ReadConsoleInput {
  public static void main(String[] args) throws IOException {
    // 一. Scanner标准的输入流形式
    //    Scanner sc = new Scanner(System.in);
    //    System.out.println("请输入姓名：");
    //    String name = sc.nextLine();
    //    System.out.println("请输入年龄：");
    //    int age = sc.nextInt();
    //    System.out.println("请输入性别：");
    //    String sex = sc.next();
    //    System.out.println("姓名：" + name + "，年龄：" + age + "，性别：" + sex);
    // 2. 关闭Scanner
    //    sc.close();
    // 二、使用BufferedReader（更底层）
    BufferedReader br = null;
    try {
      String name;
      int age;
      String sex;
      // 1. 创建BufferedReader
      br = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("请输入姓名：");
      name = br.readLine();
      System.out.println("请输入年龄：");
      age = Integer.parseInt(br.readLine());
      System.out.println("请输入性别：");
      sex = br.readLine();
      System.out.println("姓名：" + name + "，年龄：" + age + "，性别：" + sex);

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (br != null) {
        // 关闭文件
        br.close();
      }
    }
  }
}
